1 using System;
2 using
System.Collections.Generic;
3 using
System.Linq;
4 using
System.Text;
5 using
System.IO;
6
7 namespace
SteganographyBMP
8 {
9     
class LSBHelper
10     {
11         
public static void Encode(FileStream inStream, byte[] message, FileStream outStream)
12         {
13             
int byteRead; //1 byte doc vao tu inStream ( phai dung kieu int do ReadByte() tra ve kieu byte)
14             
byte byteWrite; // 1 byte de viet vao ouStream
15             
int i = 0; // chay trong mang byte Message
16             
int j = 0; //chay tung bit trong 1 byte Message[i]
17             
while ((byteRead = inStream.ReadByte()) != -1) // trong khi con chua ket thuc Stream
18             {
19                 byteWrite = (
byte)byteRead; // cast (ep kieu)
20
21                 
if (i < message.Length) // thong diep van con
22                 {
23                     
byte bit = BitWise.Extract(message[i], j++); // trich 1 bit tu vi tri j tu message[i] ra
24                     BitWise.Replace(
ref byteWrite, 0, bit); // thay the bit vao vi tri 0 (LSB)
25                     
if (j == 8) { j = 0; i++; } // da trich het 8 bit cua message[i]
26                 }
27                 
//viet ra ouStream (co nhung truong hop nhung Byte cuoi khong bi thay doi
28                 outStream.WriteByte(byteWrite);
29             }
30
31             
if (i < message.Length) //i chua chay het mang Messsage[]
32                 
throw new Exception("Thong diep qua lon de giau");
33         }
34
35         
public static byte[] Decode(FileStream inStream,int length) // doc ra 1 mang byte[] co chieu dai la Length trong file Stego
36         {
37             
int byteIndex = 0; // chay trong mang byte[]
38             
int bitIndex = 0; // chay trong 8 bit cua tung byte
39             
byte[] arrResult=new byte[length];
40             
int byteRead; // byte doc vao tu file Stego
41             
while ((byteRead = inStream.ReadByte()) != -1)
42             {
43                 
byte bit = BitWise.Extract((byte)byteRead, 0); // lay ra 1 bit vi tri thu 0;
44                 
// thay the bit nay vao trong Byte result[byteIndex] hien tai,o vi tri byteIndex
45                 BitWise.Replace(
ref arrResult[byteIndex], bitIndex++, bit);
46                 
if (bitIndex == 8) // ta da thu duoc 1 byte
47                 {
48                     bitIndex =
0;
49                     byteIndex++;
50                 }
51                 
if (byteIndex == length) break; // ta da lay du noi dung thong diep
52             }
53             
return arrResult;
54         }
55     }
56 }



Chương trình mã hóa giấu tin trong ảnh bằng C# 6.562 lượt xem

Gõ tìm kiếm nhanh...